package net.pimpas.web.jsf.renderers.poolsize.html;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import net.pimpas.web.jsf.components.ajax.poolsize.UIAjaxPoolSize;
import net.pimpas.web.jsf.renderers.RendererBase;
import net.pimpas.web.jsf.util.ComponentUtils;

import org.apache.myfaces.shared_impl.renderkit.html.HTML;

public class HtmlAjaxPoolSizeRenderer extends RendererBase {

	@Override
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UIAjaxPoolSize.class)) return;
		super.initResourceManager(context, component);
		
		UIAjaxPoolSize pool = (UIAjaxPoolSize) component;
		
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement(HTML.SCRIPT_ELEM, component);
		writer.write("HTTPRequestPool.setPoolSize("+pool.getValue()+");");
		writer.endElement(HTML.SCRIPT_ELEM);
	}
}